<?php

namespace app\common\model;

use think\facade\Cache;
use think\Model;

class AdsModel extends Model
{
    protected $autoWriteTimestamp = 'datetime';
    // 类型定义
    const TYPE_CATE = [
        1 => '启动页广告',
        2 => '首页轮播广告',
        3 => '首页弹窗广告',
        4 => '动态列表广告',
        5 => '短视频广告',
        6 => '首页背景图',
        7 => '首页宣传背景海报',
        9 => '默认头像',
        10 => '直播页轮播图',
        11 => '顶部导航背景图',
        12 => '商城首页轮播图',
        13 => '主播页广告',
        14 => '直播间弹窗广告',
        15 => '直播间绑定广告',
    ];

    const TYPE_CATE_B = [
        1 => '启动页广告',
        2 => '首页轮播广告',
        6 => '首页背景图',
        10 => '直播页轮播图',
        11 => '顶部导航背景图',
        12 => '商城首页轮播图',
        13 => '主播页广告',
        14 => '直播间弹窗广告',
        15 => '直播间绑定广告',
    ];

    public static function getTypeTextAttr($value, $data)
    {
        return isset(self::TYPE_CATE[$data['type']]) ? self::TYPE_CATE[$data['type']] : '广告配置';
    }

    protected static function init()
    {
        self::afterInsert(function ($user) {
            Cache::store('redis')->rm('DEFAULT:AVATAR');
            Cache::store('redis')->rm('LIVEADVERTISE');
        });
        self::afterUpdate(function ($user) {
            Cache::store('redis')->rm('DEFAULT:AVATAR');
            Cache::store('redis')->rm('LIVEADVERTISE');
        });
        self::afterDelete(function ($user) {
            Cache::store('redis')->rm('DEFAULT:AVATAR');
            Cache::store('redis')->rm('LIVEADVERTISE');
        });
    }
}
